<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'siteNo'">
          <Tag color="green">
            {{ record.siteNo }}
          </Tag>
        </template>
      </template>
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.ADD"
          v-auth="SiteAuth.ADD"
          @click="handleCreate"
          type="primary"
        >
          新增
        </a-button>
        <a-button
          :preIcon="IconEnum.DELETE"
          v-auth="SiteAuth.DELETE"
          @click="handleDelete"
          type="primary"
          color="error"
        >
          删除
        </a-button>
        <a-button
          :preIcon="IconEnum.EXPORT"
          v-auth="SiteAuth.EXPORT"
          @click="handleExport"
          type="primary"
          color="warning"
        >
          导出
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: IconEnum.AUTH,
              tooltip: '业务授权',
              auth: SiteAuth.SINGLE,
              onClick: handleResAuthBus.bind(null, record),
              color: 'warning',
            },
            {
              icon: IconEnum.AUTH,
              tooltip: '玩法授权',
              auth: SiteAuth.SINGLE,
              onClick: handleResAuthPlay.bind(null, record),
              color: 'success',
            },
            {
              icon: IconEnum.VIEW,
              tooltip: '查看',
              auth: SiteAuth.SINGLE,
              onClick: handleView.bind(null, record),
            },
            {
              icon: IconEnum.EDIT,
              tooltip: '编辑',
              auth: SiteAuth.EDIT,
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: IconEnum.DELETE,
              tooltip: '删除',
              auth: SiteAuth.DELETE,
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <SiteModal @register="registerModal" @success="handleSuccess" />
    <SiteModalAuthBus @register="registerResAuthBusModal" @success="handleSuccess" />
    <SiteModalAuthPlay @register="registerResAuthPlayModal" />
  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import { listSiteApi, delSiteApi, exportSiteApi } from '@/api/core/site.api';
  import { useModal } from '@/components/Modal';
  import SiteModal from '@/views/core/site/SiteModal.vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { IconEnum } from '@/enums/basic';
  import { BasicTable, TableAction, useTable } from '@/components/Table';
  import { SiteAuth } from '@/auth/core/site.auth';
  import { columns, searchFormSchema } from './site.data';
  import { SiteDetailGo } from '@/enums/core/site.enum';
  import { useUserStore } from '@/store/modules/user';
  import { SitePM } from '@/model/core/site.model';
  import SiteModalAuthBus from '@/views/core/site/SiteModalAuthBus.vue';
  import SiteModalAuthPlay from '@/views/core/site/SiteModalAuthPlay.vue';
  import { Tag } from 'ant-design-vue';

  const { createMessage, createConfirm } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerResAuthBusModal, { openModal: openResAuthBusModal }] = useModal();
  const [registerResAuthPlayModal, { openModal: openResAuthPlayModal }] = useModal();

  const state = reactive<{
    ids: string[];
    idNames: string;
  }>({
    ids: [],
    idNames: '',
  });

  const [registerTable, { reload, getForm }] = useTable({
    title: '信息管理列表',
    api: listSiteApi,
    striped: false,
    useSearchForm: true,
    rowKey: 'id',
    bordered: true,
    showIndexColumn: true,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    showTableSetting: true,
    tableSetting: {
      fullScreen: true,
    },
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    rowSelection: {
      onChange: (selectedRowKeys, selectRows) => {
        state.ids = selectedRowKeys as string[];
        state.idNames = selectRows
          .map((item) => {
            return item.id;
          })
          .join(',');
      },
    },
  });

  /** 查看按钮 */
  function handleView(record: Recordable) {
    useUserStore().getRoutePath(SiteDetailGo, record?.id);
  }

  /** 新增按钮 */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /** 修改按钮 */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /** 删除按钮 */
  function handleDelete(record: Recordable) {
    const delIds = record?.id || state.ids;
    const delNames = record?.id || state.idNames;
    if (!record?.id && state.ids.length === 0) {
      createMessage.warning('请选择要操作的数据！');
    } else {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '是否确定要删除' + delNames + '?',
        onOk: () =>
          delSiteApi(delIds).then(() => {
            createMessage.success('删除' + delNames + '成功！');
            reload();
          }),
      });
    }
  }

  /** 导出按钮 */
  function handleExport() {
    const value = getForm().getFieldsValue() as SitePM;
    exportSiteApi(value);
  }

  function handleSuccess() {
    reload();
  }

  /** 业务授权 */
  function handleResAuthBus(record: Recordable) {
    openResAuthBusModal(true, {
      record,
    });
  }

  /**
   * 玩法授权
   */
  function handleResAuthPlay(record: Recordable) {
    openResAuthPlayModal(true, {
      record,
    });
  }
</script>
